package com.test.httlcilent;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.by.wx.controller.WxinController;

public class HttpclientUtil {
	
	public static Log log = LogFactory.getLog(WxinController.class);
	
	/** 
	 * url -> method -> get
	 * @param url 
	 * @return 
	 */  
	public static String getGetDateByUrl(String url){  
		String data = null;    
	    HttpClient httpClient = new HttpClient();      
	    GetMethod getMethod = new GetMethod(url);   
	    //header must set User-Agent or 405 error
	    getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0");  
	    //default stragy    
	    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());    
	    try{  
	        //excute getMethod    
	        int statusCode = httpClient.executeMethod(getMethod);  
	        if (statusCode != HttpStatus.SC_OK) {  
	            System.err.println("Method failed:" + getMethod.getStatusLine()); 
	            log.error("Method failed:" + getMethod.getStatusLine());
	        }  
	        //read content  
	        byte[] responseBody = getMethod.getResponseBody();  
	        //request content  
	        data = new String(responseBody);  
	    }catch (HttpException e){  
	        //error for http or content 
	        log.error("Please check your provided http address!");
	        data = "";  
	        e.printStackTrace();  
	    }catch(IOException e){  
	        //error for network  
	        data = "";  
	        e.printStackTrace();  
	    }finally{  
	        //release resource 
	        getMethod.releaseConnection();  
	    }  
	    return data;  
	}
	
	/** 
	 * url -> method + params -> post
	 * @param url 
	 * @param array 
	 * @return 
	 */  
	public static String getPostDateByUrl(String url,Map<String ,String> array){  
		String data = null;     
	    HttpClient httpClient = new HttpClient();       
	    PostMethod postMethod = new PostMethod(url);   
	    //set header 
	    postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0");  
	    postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
	    //handle submit params 
	    Iterator it = array.entrySet().iterator();    
	    while (it.hasNext()){  
	        Map.Entry<String,String> entry =(Map.Entry) it.next();    
	        String key = entry.getKey();    
	        String value = entry.getValue().trim();    
	        postMethod.setParameter(key,value);  
	    }  
	    //default stragy   
	    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());    
	    try{  
	        //excute postMethod    
	        int statusCode = httpClient.executeMethod(postMethod);  
	        if (statusCode != HttpStatus.SC_OK) {  
	            System.err.println("Method failed:" + postMethod.getStatusLine());  
	            log.error("Method failed:" + postMethod.getStatusLine());
	        }  
	        //read content 
	        byte[] responseBody = postMethod.getResponseBody();  
	        //handle content  
	        data = new String(responseBody);  
	    }catch (HttpException e){  
	        //error for http or content 
	        log.error("Please check your provided http address!");
	        data = "";  
	        e.printStackTrace();  
	    }catch(IOException e){  
	        //error for network 
	        data = "";  
	        e.printStackTrace();  
	    }finally{  
	        //release resource  
	        postMethod.releaseConnection();  
	    }  
	    log.info(data);
	    return data;  
	}
}
